package com.lengyu.vxminiprogram.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lengyu.vxminiprogram.common.AuthUtil;
import com.lengyu.vxminiprogram.common.BasePageParamDto;
import com.lengyu.vxminiprogram.common.HttpResultDto;
import com.lengyu.vxminiprogram.convert.ITakePeopleConverter;
import com.lengyu.vxminiprogram.dto.CarDto;
import com.lengyu.vxminiprogram.dto.TakePeopleDto;
import com.lengyu.vxminiprogram.entity.Car;
import com.lengyu.vxminiprogram.entity.TakePeople;
import com.lengyu.vxminiprogram.entity.User;
import com.lengyu.vxminiprogram.security.rest.dto.JwtUser;
import com.lengyu.vxminiprogram.service.ITakePeopleService;
import com.lengyu.vxminiprogram.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyu
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/take-people")
public class TakePeopleController {
    @Autowired
    ITakePeopleService takePeopleService;

    @Autowired
    IUserService userService;

    @Autowired
    ITakePeopleConverter takePeopleConverter;

    @PostMapping(value="/savePeople")
    public HttpResultDto<String> savePeople(@RequestBody TakePeople people){
        JwtUser user = AuthUtil.getCurrentJwtUser();
        if(StringUtils.isBlank(people.getId())){
            people.setUserId(user.getId());
            people.setVxOpenId(user.getId());
        }
        takePeopleService.saveOrUpdate(people);
        return HttpResultDto.success(people.getId());
    }


    //参数1(/user/page)指明了访问该接口需要的url，参数2®指明了访问该接口需要的权限。
    @PostMapping(value="/list")
    public HttpResultDto<List<TakePeopleDto>> page(@RequestBody JSONObject paramMapObj){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(paramMapObj != null){
            for(String key:paramMapObj.keySet()){
                if(StringUtils.isNotBlank(String.valueOf(paramMapObj.get(key)))){
                    queryWrapper.like(key,paramMapObj.get(key));
                }
            }
        }
//        queryWrapper.orderByDesc("create_time");
        List<TakePeople> entityList = takePeopleService.list(queryWrapper);
        List<TakePeopleDto> dtoList = takePeopleConverter.listEntityToDto(entityList);
        if(dtoList != null && dtoList.size() > 0){
            dtoList.forEach(i->{
                User user = userService.getById(i.getUserId());
                if(user != null){
                    i.setUserName(user.getVxNickName());
                }
            });
        }
        return HttpResultDto.success(dtoList);
    }

    @PostMapping(value="/delete")
    public HttpResultDto<String> delete(@RequestBody JSONObject json){
        List<String> ids = Arrays.asList(json.getString("ids").split(","));
        takePeopleService.removeByIds(ids);
        return HttpResultDto.success();
    }
}
